package com.hgw.easyexcel3_demo.util;

import com.alibaba.excel.util.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Description: easyExcel工具类
 *
 * @author LinHuiBa-YanAn
 * @date 2023/5/24 11:42
 */
public class EasyExcelUtil {

    /**
     * 计算英寸
     *
     * @param pound 磅值
     * @return 转换后的英寸
     */
    public static int convertPoundToInch(int pound) {
        return (int) (pound / 72.0 * 20);
    }

    /**
     * 获取标题（动态头传入）
     *
     * @param header     表头
     * @param fieldNames 字段标题
     * @return
     */
    public static List<List<String>> head(String header, List<String> fieldNames) {
        if (CollectionUtils.isEmpty(fieldNames)) {
            return new ArrayList<>();
        }
        List<List<String>> headList = fieldNames.stream().map(fieldName -> {
            List<String> head = new ArrayList<>(2);
            if (StringUtils.isNotBlank(header)) {
                head.add(header);
            }
            head.add(fieldName);
            return head;
        }).collect(Collectors.toList());
        return headList;
    }
}
